*
* All shocks
*
set varabbrev off, permanently
clear all
use epfipc2006-2019plus.dta
*
* Total expenditure
*

global listtotal gasto1-gasto12 year2-year13 ccaa2-ccaa18 income squaredm othehouse ///
       ttenure2-ttenure6 tsize2-tsize5 pdensity2-pdensity3 residshort nmembers nfemales ///
	   nforeign ndworkers n5_15 n16_24 n25_34 n35_64 n65_79 n80omas nstudies2-nstudies4 ///
	   nactive nincome labinc peninc uneinc capinc agemb femalemb foreignmb ///
	   unemployedmb inactivemb
	   
*

foreach var1 in T10 T21 T01 T12  { 
	preserve
	* Keep only couples
	keep if coupleplus==1
    *keep if post==0 & `var1'!=.
	* propensity score estimation with survey weights
    quietly logit `var1' $listtotal  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
    gen w=`var1'+((1-`var1')*(ps/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	* imposing common support
	quietly sum ps if `var1'==1
    scalar pmintreat=r(min)
	drop if ps<pmintreat & `var1'==0
	quietly sum ps if `var1'==0
    scalar pmaxuntreat=r(max)
	drop if ps>pmaxuntreat & `var1'==1
	* estimating ATETs	
	*
	/*
	gmm (eq1: dgastot - {beta1}*`var1' - {alfa1}) ///
    (eq2: dincome - {beta2}*`var1' - {alfa2}) ///
	[pweight=estweight], instruments(eq1: `var1') ///
	instruments(eq2: `var1') winitial(identity) twostep
	nlcom (/beta1)/(/beta2)
	*/
	* Table 8 and A7
	*
	forvalues cat=1/12 {	
		replace dshare`cat'=dshare`cat'*100
		gmm (eq1: dshare`cat' - {beta1}*`var1'BOYS - {gamma1}*`var1'GIRLS - {alfa1}) ///
			[pweight=estweight], instruments(eq1: `var1'BOYS `var1'GIRLS) ///
			winitial(identity) twostep
			estimates store v`cat'
			display `"`:var label dgasto`cat''"'
			nlcom (/beta1)-(/gamma1)
			*estimates store diff`cat
			local diff=r(b)[1,1]
			local se=sqrt(r(V)[1,1])
			count if `var1'BOYS==1
			local nboys=r(N)
			count if `var1'GIRLS==1
			local ngirls=r(N)
			count if `var1'BOYS==0 & `var1'GIRLS==0
			local nuntreat=r(N)
		*outreg2 using table`var1', excel dec(3) adds(diff,  `diff', se, `se', boys, `nboys', ngirls, `ngirls', nuntreat, `nuntreat')
	}	
	*etable, estimates(v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 ) mstat(N) export(myfile.xls) showstars
	*
	* Table 7
	*
    gmm (eq1: dgastot - {beta1}*`var1'BOYS - {gamma1}*`var1'GIRLS - {alfa1}) ///
    (eq2: dincome - {beta2}*`var1'BOYS - {gamma2}*`var1'GIRLS - {alfa2}) ///
	[pweight=estweight], instruments(eq1: `var1'BOYS `var1'GIRLS) ///
	instruments(eq2: `var1'BOYS `var1'GIRLS) winitial(identity) twostep
	nlcom (/beta1)-(/gamma1)
	nlcom (/beta1)/(/beta2)
	nlcom (/beta2)-(/gamma2)
	nlcom (/gamma1)/(/gamma2)
	nlcom (/beta1)/(/beta2)-(/gamma1)/(/gamma2)
	display "Number of treated males"
	count if `var1'BOYS==1 & e(sample)==1
	display "Number of treated females"
	count if `var1'GIRLS==1 & e(sample)==1
	display "Number of untreated"
	count if `var1'BOYS==0 & `var1'GIRLS==0 & e(sample)==1
	restore 
}
	

*
* The End
